﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using HotelSoft.HotelManagement_DTO;
using HotelSoft.HotelManagement_BUS;

namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmTraCuu_Phong : Form
    {

        // Define the CS_DROPSHADOW constant
        private const int CS_DROPSHADOW = 0x00020000;

        // Override the CreateParams property
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }

        Double temp = 1;
        PhongDTO dtoPhong;
        ArrayList dsPhong;
        PhongBUS busPhong = new PhongBUS();
        LoaiphongBUS busLoaiphong = new LoaiphongBUS();
        private static bool TT = false;

        public bool TIEPTAN
        {
            get { return TT; }
            set { TT = value; }
        }

        public frmTraCuu_Phong()
        {
            InitializeComponent();
        }

        private void frmTraCuu_Phong_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            LayDanhSachPhong();
            LoadCombobox();
            LamMoi();
            lblGia.Text = "0";
            if (TT == true)
                ctmEdit.Visible = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
                timer1.Enabled = false;
        }

        private void LayDanhSachPhong()
        {
            busPhong = new PhongBUS();
            dsPhong = busPhong.LayDanhSachPhong();
            LoadListView(dsPhong);
        }

        public void LoadCombobox()
        {
            DataTable tp = new DataTable();
            tp = busLoaiphong.SelectAll();
            Cbm_Loaiphong.DisplayMember = "TenLP";
            Cbm_Loaiphong.ValueMember = "TenLP";
            Cbm_Loaiphong.DataSource = tp;
        }

        public void LoadListView(ArrayList dsPhong)
        {
            lvPhong.Items.Clear();
            ListViewItem item;
            PhongDTO dt;
            for (int i = 0; i < dsPhong.Count; i++)
            {
                dt = (PhongDTO)dsPhong[i];
                item = new ListViewItem();
                item.Text = (i+1).ToString();
                item.SubItems.Add(dt.SOPHONG.ToString());
                item.SubItems.Add(dt.SOKHACHTOIDA.ToString());
                item.SubItems.Add(dt.TRANGTHAI.ToString());
                item.SubItems.Add(dt.TENLP.ToString());
                item.SubItems.Add(dt.MOTA.ToString());
                lvPhong.Items.Add(item);
            }
            lblTongso.Text = lvPhong.Items.Count.ToString();
        }

        private void Btn_TraCuu_Click(object sender, EventArgs e)
        {
            dtoPhong = new PhongDTO();

            if (Cbm_Loaiphong.Text == "")
                dtoPhong.TENLP = "";
            else
                dtoPhong.TENLP = Cbm_Loaiphong.Text;

            if (Cbm_Trangthai.Text=="")
                dtoPhong.TRANGTHAI = "";
            else
                dtoPhong.TRANGTHAI = Cbm_Trangthai.Text;

            if (Txt_Sophong.Text == "")
                dtoPhong.SOPHONG = "";
            else
                dtoPhong.SOPHONG = Txt_Sophong.Text;

            if (Txt_Sokhach.Text == "")
                dtoPhong.SOKHACHTOIDA = 0;
            else
                dtoPhong.SOKHACHTOIDA = int.Parse(Txt_Sokhach.Text);
            dtoPhong.MOTA = "";
            ArrayList dsTraCuuDuoc = busPhong.TraCuuPhong(dsPhong, dtoPhong);
            LoadListView(dsTraCuuDuoc);
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            frmQuanly_PHONG frm = new frmQuanly_PHONG();
            frm.MAPHONG = lvPhong.FocusedItem.SubItems[1].Text;
            frm.ShowDialog();
        }

        private void lvKhachHang_MouseHover(object sender, EventArgs e)
        {
            balloonTip1.DefaultBalloonWidth = 100;
            balloonTip1.SetBalloonText(lvPhong, "Hãy bấm chuột phải để thực hiện chức năng!");
            balloonTip1.ShowBalloon(lvPhong);
        }

        private void ctmDatphong_Click(object sender, EventArgs e)
        {
            frmDat_PHONG frm = new frmDat_PHONG();
            frm.ShowDialog();
        }

        private void ctmTraphong_Click(object sender, EventArgs e)
        {
            dtoPhong = new PhongDTO();
            dtoPhong.SOPHONG = lvPhong.FocusedItem.SubItems[1].Text;
            dtoPhong.TRANGTHAI = lvPhong.FocusedItem.SubItems[3].Text;
            mainTiepTanBUS busmainTT = new mainTiepTanBUS();
            DataTable tbl = new DataTable();
            tbl = busmainTT.getSoPDK(dtoPhong);
            if (tbl.Rows.Count == 0)
            {
                MessageBox.Show("Tình trạng phòng đang thuê\nTuy nhiên chưa có phiếu đăng ký\nXin vui lòng kiểm tra lại!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            frmTra_PHONG frm = new frmTra_PHONG();
            frm.SOPHIEUDK = tbl.Rows[0].ItemArray[0].ToString();
            this.Hide();
            frm.ShowDialog();
            frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
        }

        void frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Show();
            btnReset_Click(sender, e);
        }

        private void ctMenu_Paint(object sender, PaintEventArgs e)
        {
            if (lvPhong.FocusedItem.SubItems[3].Text == "Trống")
            {
                this.ctmTraphong.Visible = false;
                this.ctmDatphong.Visible = true;
            }
            else
            {
                this.ctmTraphong.Visible = true;
                this.ctmDatphong.Visible = false;
            }
        }

        private void Cbm_Loaiphong_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Cbm_Loaiphong.SelectedIndex != -1)
            {
                DataTable tbl = new DataTable();
                LoaiPhongDTO dto = new LoaiPhongDTO();
                dto.TENLP = Cbm_Loaiphong.SelectedValue.ToString();
                tbl = busLoaiphong.SelectAll_DieuKien(dto);
                Decimal gia = 0;
                gia = Decimal.Parse(tbl.Rows[0].ItemArray[1].ToString());
                lblGia.Text = gia.ToString("#,##0") + " VNĐ";
            }
        }

        private void Cbm_Loaiphong_TextChanged(object sender, EventArgs e)
        {
            if (Cbm_Loaiphong.Text == "")
                lblGia.Text = "0";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void LamMoi()
        {
            Txt_Sophong.Text = "";
            Txt_Sokhach.Text = "";
            Cbm_Trangthai.SelectedIndex = -1;
            Cbm_Loaiphong.SelectedIndex = -1;
        }
        private void btnReset_Click(object sender, EventArgs e)
        {
            LamMoi();
            LayDanhSachPhong();
        }

        private void lvPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvPhong.FocusedItem != null)
            {
                Txt_Sophong.Text = lvPhong.FocusedItem.SubItems[1].Text;
                Txt_Sokhach.Text = lvPhong.FocusedItem.SubItems[2].Text;
                Cbm_Trangthai.Text = lvPhong.FocusedItem.SubItems[3].Text;
                Cbm_Loaiphong.Text = lvPhong.FocusedItem.SubItems[4].Text;
                
            }
        }

        private void btnInDS_Click(object sender, EventArgs e)
        {
            frmReport_Phong frm = new frmReport_Phong();
            frm.ShowDialog();
        }

        private void Txt_Sokhach_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
